<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'misc/inet_functions.inc.php');


abstract class Mailer_Simple extends Tangra_Class {
	private $log_file = '';
	private $headers = array();
	private $subject;
	private $body;
	private $encoding = 'utf-8';
	private $tos = array();
	private $from;


	function __construct($log_file = '') {
		$this->set_log_file($log_file);
	}


	public function set_log_file($log_file = '') {
		if ($log_file != '' && $log_file != NULL) {
			$dir = dirname($log_file);
			if (file_exists($dir)) {
				$this->log_file = $log_file;
			} else {
				throw new Tangra_Module_Exception('Directory of the log file does not exist: '.$dir);
			}
		} else {
			$this->log_file = $log_file;
		}
	}


	public function get_log_file() {
		return $this->log_file;
	}


	public function add_header($header) {
		$ret = false;

		if (trim($header)) {
			if (!$this->header_exists($header)) {
				$this->headers[] = $header;
				$ret = true;
			}
		}

		return $ret;
	}


	public function remove_header($header, $forgiving = true) {
		$ret = false;

		$index = $this->header_exists($header);
		if ($index !== false) {
			unset($this->headers[$index]);
			$ret = true;
		} else {
			if (!$forgiving) {
				throw new Tangra_Exception('No such header: '.$header);
			}
		}

		return $ret;
	}


	public function header_exists($header) {
		$ret = false;

		foreach($this->headers as $key => $added_header) {
			if ($added_header == $header) {
				$ret = $key;
				break;
			}
		}

		return $ret;
	}


	public function get_headers() {
		return $this->headers;
	}


	public function set_subject($subject) {
		$this->subject = $subject;
	}


	public function get_subject() {
		return $this->subject;
	}


	public function set_body($body, $encoding = 'utf-8') {
		$this->body = $body;
		$this->set_encoding($encoding);
	}


	public function get_body() {
		return $this->body;
	}


	public function set_encoding($encoding) {
		$this->encoding = $encoding;
	}


	public function get_encoding() {
		return $this->encoding;
	}


	public function to_exists($to) {
		$ret = false;

		foreach ($this->tos as $key => $to_local) {
			if ($to_local == $to) {
				$ret = $key;
				break;
			}
		}

		return $ret;
	}


	public function add_to($to) {
		$ret = false;

		if ($this->to_exists($to) === false) {
			if (is_valid_email_rfc2822($to)) {
				$this->tos[] = $to;
			} else {
				throw new Tangra_Module_Exception('Invalid email: '.$to);
			}
		}

		return $ret;
	}


	public function remove_to($to) {
		$index = $this->to_exists($to);
		if ($index !== false) {
			unset($this->tos[$index]);
		}

	}


	public function get_tos() {
		return $this->tos;
	}


	public function set_from($from) {
		if (is_valid_email_rfc2822($from)) {
			$this->from = $from;
		} else {
			throw new Tangra_Module_Exception('Invalid email: '.$from);
		}
	}


	public function get_from() {
		return $this->from;
	}


	protected function log_this($msg) {
		if ($this->get_log_file()) {
			error_log(date('Y-m-d H:i:s').' - '.$msg."\r\n", 3, $this->get_log_file());
		}
	}


	protected function log_sent($status) {
		if ($this->get_log_file()) {
			$to = '';
			foreach($this->get_tos() as $recip) {
				$to .= $recip. '  ';
			}

			$status = $status ? '+OK+++++' : '-FAILED-';
			$this->log_this('Status: '.$status.'   From: '.$this->get_from()."   To: ".$to);
		}
	}


	abstract public function send();
}